package MyApache2::Wykres;


use FinanceChart;
use strict;
use warnings;

use Apache2::Request ();
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile => qw(OK);

my	$dbh = DBI->connect("DBI:mysql:test:localhost", '', '',) or die "Cannot connect to database:  $DBI::errstr";

sub	handler {
	my $r=shift;
	my $uri=$r->uri;
	$uri=~s/\/wykres\///g;

my	$sth=$dbh->prepare(qq{select * from dane where ticker="$uri" and data like '20091%'} );
	$sth->execute();

my	(@ts,@open,@high,@low,@close,@vol);


while(my @row=$sth->fetchrow_array) {
#	$row[1]=~s/200911//g;
#	$row[1]=~s/^0//g;
	push(@ts,$row[1]);
	push(@open,$row[2]);
	push(@high,$row[3]);
	push(@low,$row[4]);
	push(@close,$row[5]);
	push(@vol,$row[6]);
	
#	print "$row[0] $row[1] $row[2] $row[3] $row[4] $row[5] $row[6]\n";
}

my	$c = new FinanceChart(640);
	$c->addTitle("$uri - last quotation: $ts[$#ts]");
	$c->setData(\@ts,\@high,\@low,\@open,\@close,\@vol);
	$c->addMainChart(300);
	$c->addSimpleMovingAvg(10, 0x663300);
	$c->addCandleStick(0x00ff00, 0xff0000);
#	$c->addDonchianChannel(20, 0x9999ff, 0xc06666ff);
	$c->addRSI(75, 14, 0x800080, 20, 0xff0000, 0x0000ff);
	$c->addMACD(75, 26, 12, 9, 0x0000ff, 0xff00ff, 0x008000);
	$c->addVolBars(75, 0x99ff99, 0xff9999, 0x808080);
	binmode(STDOUT);
	$r->content_type('image/png');
	$r->print( $c->makeChart2($perlchartdir::PNG) );

	return Apache2::Const::OK;
}
1;
